package com.xuecheng.content.controller;

import com.xuecheng.commons.model.vo.CoursePubVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.content.domain.CoursePub;
import freemarker.template.TemplateException;
import org.springframework.web.bind.annotation.*;
import com.xuecheng.content.service.CoursePubService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.util.Map;

/**
 * <p>
 * 课程发布前端控制器
 * </p>
 *
 * @author itheima
 */
@Slf4j
@RestController
public class CoursePubController {

    @Autowired
    private CoursePubService  coursePubService;
    // 课程预览（同步请求）
    @GetMapping("/coursePub/preview/{courseBaseId}")
    public ModelAndView preview(@PathVariable("courseBaseId") Long courseBaseId) {
        // 调用service
        Map model = coursePubService.preview(courseBaseId);

        // 模型 + 模板=输出
        return new ModelAndView("learing_article", model);
    }
    // 课程发布（将课程保存到数据库 minio 同步es）
    @PostMapping("/coursePub/publish/{courseBaseId}")
    public ResponseResult publish(@PathVariable("courseBaseId") Long courseBaseId) throws TemplateException, IOException {
        return coursePubService.publish(courseBaseId);
    }
    // 根据id查询课程发布对象
    @GetMapping("/coursePub/{id}")
    public ResponseResult<CoursePubVo> findById(@PathVariable("id") Long id) {
        // 调用service查询
        CoursePub coursePub = coursePubService.getById(id);
        // 转vo
        CoursePubVo vo = BeanHelper.copyProperties(coursePub, CoursePubVo.class);
        // 返回vo
        return ResponseResult.okResult(vo);
    }
    //课程下架（删除数据库课程发布对象 minio 和es数据 ）
    @GetMapping("/course/offline/{id}")
    public ResponseResult offline (@PathVariable("id") Long courseBaseId){
        return coursePubService.offline(courseBaseId);

    }
}
